using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

namespace HYOK
{
    public partial class GameQualityMgr
    {
        private static GameQualityMgr _m_instance;

        public static GameQualityMgr instance
        {
            get
            {
                if(_m_instance == null)
                {
                    _m_instance = new GameQualityMgr();
                }
                return _m_instance;
            }
        }
        
        private bool _m_openInstancing = true;
        private bool _m_bOpenShadow = true;//是否开启阴影
        private bool _m_bOpenShowcaseShadow = true;//是否开启阴影
        private bool _m_bOpenLowResolution = false;//是否开启低分辨率
        private bool _m_bIsInitResolution = false;
        private Vector2Int _m_vScreenSize;
        private float _m_fLowResolutionScale;
        private bool _m_bOpenPostProcess = false;//是否开启后效
        private bool _m_bOpenClothPhysics = true;//是否开启布料物理
        private bool _m_bIsRequireDepthTexture = false;//是否开启深度图写入

        public bool isOpenShadow => _m_bOpenShadow;
        public bool isOpenShowcaseShadow => _m_bOpenShowcaseShadow;
        public bool isOpenLowResolution => _m_bOpenLowResolution;
        public bool isOpenPostProcess => _m_bOpenPostProcess;
        public bool isOpenClothPhysics => _m_bOpenClothPhysics;
        public bool isRequireDepthTexture => _m_bIsRequireDepthTexture;

        public void setQualityLevel(EGameViewQuality _level)
        {

            switch (_level)
            {
                case EGameViewQuality.LOW:
                    setQualityLow();
                    break;
                case EGameViewQuality.NORMAL:
                    setQualityMedium();
                    break;
                case EGameViewQuality.HIGH:
                    setQualityHigh();
                    break;
            }

            //Game.instance.mainCamera.setToOverlayCamera();
            updateQualitySetting();
            _setGraphicSetting();
        }

        private void setQualityLow()
        {
            _m_bOpenShadow = false;//是否开启阴影
            _m_bOpenShowcaseShadow = false;
            _m_bOpenLowResolution = true;//是否开启低分辨率
            _m_bOpenPostProcess = false;//是否开启后效
            _m_bOpenClothPhysics = false;
            _m_bIsRequireDepthTexture = false;
            //GraphicsSettings.renderPipelineAsset = Game.instance.mainCamera.urpSetting.lowRenderPipelineAsset;
            //QualitySettings.renderPipeline = Game.instance.mainCamera.urpSetting.lowRenderPipelineAsset;
            _updateURPSetting();
            // // Overlay相机都改为base，可以直接渲染到手机buffer里
            // NPGame.instance.mainCamera.mainCameraData.cameraStack.Clear();
            // NPGame.instance.mainCamera.uiCameraData.renderType = CameraRenderType.Base;
            // NPGame.instance.mainCamera.rtMainCameraData.renderType = CameraRenderType.Base;
            Shader.globalMaximumLOD = 200;
        }
        private void setQualityMedium()
        {
            _m_bOpenShadow = true;//是否开启阴影
            _m_bOpenShowcaseShadow = true;
            _m_bOpenLowResolution = true;//是否开启低分辨率
            _m_bOpenPostProcess = false;//是否开启后效
            _m_bOpenClothPhysics = true;
            _m_bIsRequireDepthTexture = true;
            //GraphicsSettings.renderPipelineAsset = Game.instance.mainCamera.urpSetting.mediumRenderPipelineAsset;
            //QualitySettings.renderPipeline = Game.instance.mainCamera.urpSetting.mediumRenderPipelineAsset;
            _updateURPSetting();
            // NPGame.instance.mainCamera.uiCameraData.renderType = CameraRenderType.Overlay;
            // NPGame.instance.mainCamera.rtMainCameraData.renderType = CameraRenderType.Overlay;
            // NPGame.instance.mainCamera.mainCameraData.cameraStack.Clear();
            // NPGame.instance.mainCamera.mainCameraData.cameraStack.Add(NPGame.instance.mainCamera.rtMainCamera);
            // NPGame.instance.mainCamera.mainCameraData.cameraStack.Add(NPGame.instance.mainCamera.uiCamera);
            Shader.globalMaximumLOD = 300;
        }     
        private void setQualityHigh()
        {
            _m_bOpenShadow = true;//是否开启阴影
            _m_bOpenShowcaseShadow = true;
            _m_bOpenLowResolution = false;//是否开启低分辨率
            _m_bOpenPostProcess = false;//是否开启后效
            _m_bOpenClothPhysics = true;
            _m_bIsRequireDepthTexture = true;
            //GraphicsSettings.renderPipelineAsset = Game.instance.mainCamera.urpSetting.highRenderPipelineAsset;
            //QualitySettings.renderPipeline = Game.instance.mainCamera.urpSetting.highRenderPipelineAsset;
            _updateURPSetting();
            // NPGame.instance.mainCamera.uiCameraData.renderType = CameraRenderType.Overlay;
            // NPGame.instance.mainCamera.rtMainCameraData.renderType = CameraRenderType.Overlay;
            // NPGame.instance.mainCamera.mainCameraData.cameraStack.Clear();
            // NPGame.instance.mainCamera.mainCameraData.cameraStack.Add(NPGame.instance.mainCamera.rtMainCamera);
            // NPGame.instance.mainCamera.mainCameraData.cameraStack.Add(NPGame.instance.mainCamera.uiCamera);
            Shader.globalMaximumLOD = 600;
        }

        /// <summary>
        /// 刷新更新urp设置后需要更新的设置
        /// </summary>
        private void _updateURPSetting()
        {
            // var urpAsset = GraphicsSettings.renderPipelineAsset as UniversalRenderPipelineAsset;
            // if (urpAsset != null )
            // {
            //     urpAsset.supportsCameraDepthTexture = _m_bIsRequireDepthTexture;
            // }
        }

        private void updateQualitySetting()
        {
            //LightsMgr.instance.updateShadowType();
            updateIsLowResolution();
            //MagicaPhysicsManager.Instance.enabled = _m_bOpenClothPhysics;
        }
        
        /// <summary>
        /// 根据GameSetting设置低分辨率
        /// </summary>
        private void updateIsLowResolution()
        {
            if(!_m_bIsInitResolution)
            {
                _m_vScreenSize = new Vector2Int(Screen.width, Screen.height);
                _m_fLowResolutionScale = 0.6667f;
                _m_bIsInitResolution = true;
            }
#if UNITY_STANDALONE && !UNITY_EDITOR
#else
            if(isOpenLowResolution)
            {
                Screen.SetResolution((int)(_m_vScreenSize.x * _m_fLowResolutionScale), (int)(_m_vScreenSize.y*_m_fLowResolutionScale), Screen.fullScreen);
            }
            else
            {
                Screen.SetResolution(_m_vScreenSize.x, _m_vScreenSize.y, Screen.fullScreen);
            }
#endif
        }
        
        private void _setGraphicSetting()
        {
            bool supportInstance = SystemInfo.supportsInstancing;

            // 由于模拟器 给到的是否支持Instancing的数据是错的，
            // 另外由于ShaderModel 3.5的机器支持Instancing的比较少，所以这里直接判断shaderModel 3.5及以下的都不开启Instancing，确保显示正常
            if (SystemInfo.graphicsShaderLevel <= 35)
            {
                supportInstance = false;
            }

            _m_openInstancing = supportInstance;
        }
    }
}